package com.starlinkdt.service.system.impl;

import com.starlinkdt.common.constant.HttpStatus;
import com.starlinkdt.common.domain.system.dto.OaiServDto;
import com.starlinkdt.common.domain.system.entity.OaiServ;
import com.starlinkdt.common.enums.DisableStatus;
import com.starlinkdt.common.exception.ServiceException;
import com.starlinkdt.common.mapper.system.OaiPermMapper;
import com.starlinkdt.common.mapper.system.OaiServMapper;
import com.starlinkdt.common.utils.JSONUtils;
import com.starlinkdt.common.utils.MyStringUtils;
import com.starlinkdt.common.utils.SecurityUtils;
import com.starlinkdt.service.system.IOaiServService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * OAI服务Service业务层处理
 * 
 * @author system
 */
@Service
public class OaiServServiceImpl implements IOaiServService
{
    @Autowired
    private OaiServMapper oaiServMapper;

    @Autowired
    private OaiPermMapper oaiPermMapper;

    private static final String servBizNoPre  = "UOS";

    private static final int servBizNoLength = 20;

    /**
     * 查询OAI服务列表
     *
     * @param oaiServSearch OAI服务
     * @return OAI服务集合
     */
    @Override
    public List<OaiServ> selectOaiServList(OaiServ oaiServSearch) {
        return oaiServMapper.selectOaiServList(oaiServSearch);
    }

    /**
     * 查询OAI服务
     *
     * @param servBizNo OAI服务唯一编码
     * @return OAI服务
     */
    @Override
    public OaiServ selectOaiServByServBizNo(String servBizNo) {
        OaiServ oaiServ = oaiServMapper.selectOaiServByServBizNo(servBizNo);
        if (oaiServ == null) {
            throw new ServiceException("OAI服务不存在", HttpStatus.CONFLICT);
        }
        return oaiServ;
    }

    /**
     * 新增OAI服务
     *
     * @param oaiServDto OAI服务
     * @return 结果
     */
    @Override
    public int insertOaiServ(OaiServDto oaiServDto) {
        // 验证重复请求
        if (oaiServMapper.selectOaiServByServApi(oaiServDto.getServApi()) != null) {
            throw new ServiceException("OAI服务已存在", HttpStatus.CONFLICT);
        }

        // 数据处理
        OaiServ oaiServ = JSONUtils.parseObject(JSONUtils.toJSONString(oaiServDto), OaiServ.class);
        oaiServ.setServBizNo(MyStringUtils.genBizNo(servBizNoLength, servBizNoPre));
        oaiServ.setCreateBy(SecurityUtils.getUsername());

        return oaiServMapper.insertOaiServ(oaiServ);
    }

    /**
     * 修改OAI服务
     *
     * @param oaiServDto OAI服务
     * @return 结果
     */
    @Override
    public int updateOaiServByServBizNo(OaiServDto oaiServDto) {
        // 检查OAI服务
        if (oaiServDto.getServBizNo() == null || oaiServDto.getServBizNo().isEmpty()) {
            throw new ServiceException("OAI服务不存在", HttpStatus.CONFLICT);
        }
        OaiServ oaiServExist = oaiServMapper.selectOaiServByServBizNo(oaiServDto.getServBizNo());
        if (oaiServExist == null) {
            throw new ServiceException("OAI服务不存在", HttpStatus.CONFLICT);
        }

        // 数据处理
        OaiServ oaiServ = JSONUtils.parseObject(JSONUtils.toJSONString(oaiServDto), OaiServ.class);
        oaiServ.setUpdateBy(SecurityUtils.getUsername());

        return oaiServMapper.updateOaiServByServBizNo(oaiServ);
    }

    /**
     * 启用OAI服务
     *
     * @param servBizNos OAI服务唯一编码
     * @return 结果
     */
    @Override
    public int enableOaiServByServBizNos(String[] servBizNos) {
        return oaiServMapper.updateOaiServStatusByServBizNos(servBizNos, DisableStatus.ENABLE.getType(), SecurityUtils.getUsername());
    }

    /**
     * 禁用OAI服务
     *
     * @param servBizNos OAI服务唯一编码
     * @return 结果
     */
    @Override
    public int disableOaiServByServBizNos(String[] servBizNos) {
        return oaiServMapper.updateOaiServStatusByServBizNos(servBizNos, DisableStatus.DISABLE.getType(), SecurityUtils.getUsername());
    }

    /**
     * 删除OAI服务
     *
     * @param servBizNo OAI服务唯一编码
     * @return 结果
     */
    @Override
    public int deleteOaiServByServBizNo(String servBizNo) {
        if (oaiServMapper.selectOaiServByServBizNo(servBizNo) == null) {
            throw new ServiceException("OAI服务不存在", HttpStatus.CONFLICT);
        }
        if (!oaiPermMapper.selectOaiPermByServBizNos(new String[]{servBizNo}).isEmpty()) {
            throw new ServiceException("OAI服务已配置权限，禁止删除", HttpStatus.CONFLICT);
        }

        return oaiServMapper.deleteOaiServByServBizNo(servBizNo, SecurityUtils.getUsername());
    }

    /**
     * 批量删除OAI服务
     *
     * @param servBizNos OAI服务唯一编码
     * @return 结果
     */
    @Override
    public int deleteOaiServByServBizNos(String[] servBizNos) {
        if (!oaiPermMapper.selectOaiPermByServBizNos(servBizNos).isEmpty()) {
            throw new ServiceException("OAI服务已配置权限，禁止删除", HttpStatus.CONFLICT);
        }

        return oaiServMapper.deleteOaiServByServBizNos(servBizNos, SecurityUtils.getUsername());
    }

}
